package com.qcby.hengxiang.learn.rbac;

import com.qcby.hengxiang.learn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class HeaboyUserDetailsService implements UserDetailsService {
    @Autowired
    private UserService userService;

    /**
     * 需新建配置类注册一个指定的加密方式Bean，或在下一步Security配置类中注册指定
     */
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        com.qcby.hengxiang.learn.entity.User user = userService.getUser(username);
        if(user!=null){
            // 角色集合
            List<GrantedAuthority> authorities = new ArrayList<>();
            // 角色必须以`ROLE_`开头，数据库中没有，则在这里加
            String role="admin";
            authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
            return new User(
                    user.getUsername(),
                    // 因为数据库是明文，所以这里需加密密码
                    passwordEncoder.encode(user.getPasswd()),
                    authorities);

        }else {
            throw new UsernameNotFoundException("查无此人！");
        }


    }
}
